﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;

namespace FFmpegVideoClip.Lib.AdvancedFragmentProcessor
{
    public static class InputParameterItemSelectTools
    {
        public static string? TextSelect(string MatcheItem, MatchCollection matchCollection) => MatcheItem switch
        {
            "crop" => CropSelect(matchCollection.Where(p => p.Success)),
            _ => matchCollection.Where(p => p.Success).LastOrDefault()?.Value.Trim('"', ',', ':', '.', ';', '!')
        };

        public static string CropSelect(IEnumerable<Match> matchCollection)
        {
            (int width, int height, int StartX, int StartY)[] arr = matchCollection.Select(p =>
            {
                int[] ints = p.Value.Split(':', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Select(p => int.Parse(p)).ToArray();
                return (width: ints[0], height: ints[1], StartX: ints[2], StartY: ints[3]);
            }).ToArray();
            int top = int.MaxValue, bottom = 0, left = int.MaxValue, right = 0, temp;
            foreach ((int width, int height, int StartX, int StartY) item in arr)
            {
                // 取top最小值
                if (top > item.StartY)
                    top = item.StartY;

                // 取left最小值   
                if (left > item.StartX)
                    left = item.StartX;

                // 取bottom最大值
                temp = item.StartY + item.height;
                if (bottom < temp)
                    bottom = temp;

                // 取right最大值
                temp = item.StartX + item.width;
                if (right < temp)
                    right = temp;
            }
            return $"{right-left}:{bottom-top}:{left}:{top}";
        }
    }
}